/**
 * Contains custom JavaScript code
 */
var urlHolderP = new Object();

function loadTableP() {
	$.get(urlHolderP.records, function(response) {
		
 		$('#tableUsersP').find('tbody').remove();
 		
 		for (var i=0; i<response.users.length; i++) {
			var row = '<tr>';
			row += '<td><input type="radio" name="index" id="index" value="'+i+'"></td>';
			row += '<td>' + response.users[i].username + '</td>';
			row += '<td>' + response.users[i].firstName + '</td>';
			row += '<td>' + response.users[i].lastName + '</td>';
			row += '<td>' + getProjectP(response.users[i].project) + '</td>';
			row += '</tr>';
	 		$('#tableUsersP').append(row);
 		}
 		
 		$('#tableUsersP').data('model', response.users);
 		toggleFormsP('hide'); ;
		
 	});
	//toggleFormsP('hide'); ;
}

function submitNewRecordP() {
	$.post(urlHolderP.add, {
			username: $('#newUsernameP').val(),
			password: $('#newPasswordP').val(),
			firstName: $('#newFirstNameP').val(),
			lastName: $('#newLastNameP').val(),
			role: $('#newRoleP').val(),
			project :$('#newProjectP').val()
		}, 
		function(response) {
			if (response != null) {
				loadTableP();
				toggleFormsP('hide'); ;
				toggleCrudButtonsP('show');
				alert('Success! Record has been added.');
			} else {
				alert('Failure! An error has occurred!');
			}
		}
	);	
}

function submitDeleteRecordP() {
	var selected = $('input:radio[name=index]:checked').val();
	
	$.post(urlHolderP.del, {
			username: $('#tableUsersP').data('model')[selected].username
		}, 
		function(response) {
			if (response == true) {
				loadTableP(urlHolderP.records);
				alert('Success! Record has been deleted.');
			} else {
				alert('Failure! An error has occurred!');
			}
		}
	);
}

function submitUpdateRecordP() {
	$.post(urlHolderP.edit, {
			username: $('#editUsernameP').val(),
			firstName: $('#editFirstNameP').val(),
			lastName: $('#editLastNameP').val(),
			role: $('#editRoleP').val(),
			project:$('#editProjectP').val()
		}, 
		function(response) {
			if (response != null) {
				loadTableP();
				toggleFormsP('hide'); ;
				toggleCrudButtonsP('show');
				alert('Success! Record has been edited.');
			} else {
				alert('Failure! An error has occurred!');
			}
		}
	);
}

function getProjectP(project) {
	if (project == 11) {
		return 'Bench';
	} 
	else if (project == 1) {
		return 'P1';
	} 
	else if (project == 2) {
		return 'P2';
	} 
	else if (project == 3) {
		return 'P3';
	}
	else if (project == 4) {
		return 'P4';
	}
	else if (project == 5) {
		return 'P5';
	}
	else if (project == 6) {
		return 'P6';
	}
	else if (project == 7) {
		return 'P7';
	}
	else if (project == 8) {
		return 'P8';
	}
	else if (project == 9) {
		return 'P9';
	}
	else {
		return 'Unknown';
	} 
}

function hasSelectedP() {
	var selected = $('input:radio[name=index]:checked').val();
	if (selected == undefined) { 
		alert('Select a record first!');
		return false;
	}
	
	return true;
}

function fillEditFormP() {
	loadProjectIds();
	var selected = $('input:radio[name=index]:checked').val();
	$('#editUsernameP').val( $('#tableUsersP').data('model')[selected].username );
	$('#editFirstNameP').val( $('#tableUsersP').data('model')[selected].firstName );
	$('#editLastNameP').val( $('#tableUsersP').data('model')[selected].lastName );
	$('#editRoleP').val( $('#tableUsersP').data('model')[selected].role );
}

function resetNewFormP() {
	$('#newUsernameP').val('');
	$('#newPasswordP').val('');
	$('#newFirstNamP').val('');
	$('#newLastNameP').val('');
	$('#newRoleP').val('2');
}

function resetEditFormP() {
	$('#editFirstNameP').val('');
	$('#editLastNameP').val('');
	$('#editRoleP').val('2');
}

function toggleFormsP(id) {
	
	if (id == 'hide') {
		$('#newFormP').hide();
		$('#editFormP').hide();
		
	} else if (id == 'new') {
 		resetNewFormP();
 		$('#newFormP').show();
 		$('#editFormP').hide();
 		
	} else if (id == 'edit') {
		
 		resetEditFormP();
 		$('#newFormP').hide();
 		$('#editFormP').show();
	}
}

function toggleCrudButtonsP(id) {
	if (id == 'show') {
		$('#newBtnP').removeAttr('disabled');
		$('#editBtnP').removeAttr('disabled');
		$('#deleteBtnP').removeAttr('disabled');
		$('#reloadBtnP').removeAttr('disabled');
		
	} else if (id == 'hide'){
		$('#newBtnP').attr('disabled', 'disabled');
		$('#editBtnP').attr('disabled', 'disabled');
		$('#deleteBtnP').attr('disabled', 'disabled');
		$('#reloadBtnP').attr('disabled', 'disabled');
	}
}

function loadProjectIds() {
	

	$.get(urlHolderP.populate, function(response) {
		$('#editProjectP').val('');
		var list ;
		for (var i=0; i<response.projects.length; i++) {
			list += '<option value='+(i+1)+'>'+response.projects[i].projectId+'</option>'
			//$('#editProjectP').append('<option value='+(i+1)+'>'+response.projects[i].projectId+'</option>');
		}
		$('#editProjectP').html(list);
		});
	
}
